import tkinter as tk
from tkinter import ttk, messagebox
from GUI界面.AuthWindow import AuthWindow
from GUI界面.AboutWindow import AboutWindow
from GUI界面.HomeWindow import HomeWindow
from GUI界面.BlogList import BlogList
from GUI界面.BlogManage import BlogManage
from GUI界面.FansManage import FansManage
from GUI界面.UserManage import UserManage
from 数据库操作.UpdatePassword import UpdatePassword


class MainApplication(tk.Tk):
    """主程序基础框架"""
    def __init__(self, User):
        super().__init__()
        self.entry_var = None
        self.popup = None
        self.title("个人博客系统")
        self.geometry("800x600")

        self.user_data = User

        # 创建主容器
        self.main_frame = tk.Frame(self)
        self.main_frame.pack(fill=tk.BOTH, expand=True)

        # 创建顶部导航栏
        self.create_navbar()

        # 创建内容容器
        self.content_frame = tk.Frame(self.main_frame)
        self.content_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)

        # 当前显示的子窗口
        self.current_subwindow = None

        # 初始化加载首页
        self.show_subwindow(HomeWindow)

    def create_navbar(self):
        """创建顶部导航按钮栏"""
        navbar = tk.Frame(self.main_frame)
        navbar.pack(fill=tk.X, padx=5, pady=5)

        user_frame = ttk.Frame(navbar)
        user_frame.pack(side=tk.RIGHT)
        ttk.Button(user_frame, text="退出登录",
                   command=self.logout).pack(side=tk.RIGHT, padx=5)
        ttk.Button(user_frame, text="修改密码",
                   command=self.update_password).pack(side=tk.RIGHT, padx=5)


        buttons = [
            ("首页", HomeWindow),
            ("博客列表", BlogList),
            ("博客管理", BlogManage),
            ("粉丝管理", FansManage),
            ("用户管理", UserManage),
            ("关于我", AboutWindow)
        ]

        for text, command in buttons:
            btn = ttk.Button(navbar, text=text,
                             command=lambda cls=command: self.show_subwindow(cls))
            btn.pack(side=tk.LEFT, padx=5)

    def logout(self):
        """退出登录"""
        self.destroy()
        AuthWindow()  # 重新显示登录窗口

    def update_password(self):
        """弹出修改密码的小窗口"""
        # 创建小窗口
        self.popup = tk.Toplevel(self)
        self.popup.title("修改密码")
        self.popup.geometry("300x150")

        #  添加一个展示要求文字的文本框
        ttk.Label(self.popup, text=f"你好！{self.user_data.username}").pack()
        ttk.Label(self.popup, text="请输入新密码：").pack()

        # 添加输入框
        self.entry_var = tk.StringVar()
        entry = ttk.Entry(self.popup, textvariable=self.entry_var, show="*")  # 密码输入框
        entry.pack(pady=10)

        # 按钮框架
        button_frame = ttk.Frame(self.popup)
        button_frame.pack()

        # 提交按钮
        submit_button = ttk.Button(button_frame, text="提交", command=self.submit_password)
        submit_button.pack(side=tk.LEFT, padx=5)

        # 返回按钮
        cancel_button = ttk.Button(button_frame, text="返回", command=self.popup.destroy)
        cancel_button.pack(side=tk.LEFT, padx=5)

    def submit_password(self):
        """修改密码"""
        if UpdatePassword().update_password(self.user_data.username, self.entry_var.get()):
            messagebox.showinfo("成功", "修改成功！")
        else:
            messagebox.showinfo("失败", "修改失败！")
        self.popup.destroy()
    def on_close(self):
        """关闭窗口时退出程序"""
        self.destroy()
        self.quit()

    def show_subwindow(self, window_class):
        """显示子窗口"""
        if self.current_subwindow:
            self.current_subwindow.destroy()

        self.current_subwindow = window_class(self.content_frame, user=self.user_data)
        self.current_subwindow.pack(fill=tk.BOTH, expand=True)

if __name__ == "__main__":
    test = MainApplication("")
    test.mainloop()
